package com.fanco.learn_datastruct_leetcode.leetcode;

/**
 * 给你一个链表数组，每个链表都已经按升序排列。
 *
 * 请你将所有链表合并到一个升序链表中，返回合并后的链表。
 *
 * 1. 思路1： 循环遍历，前后节点合并。
 * 2. 思路2： 分治 再合并。拆分为多组链表合并
 *
 * [[],[-1,5,11],[],[6,10]]
 *
 * @author qiaowang 2021/7/6
 */
public class Day15 {
    public static void main(String[] args) {
        Solution solution = new Solution();

        ListNode l1 = new ListNode(-1, new ListNode(5, new ListNode(11)));
        ListNode l2 = new ListNode(6, new ListNode(10));

        ListNode[] lists = {null,l1,null, l2};
        ListNode listNode = solution.mergeKLists(lists);
        while (listNode != null) {
            System.out.print(listNode.val + "\t");
            listNode = listNode.next;
        }

    }

    private static class Solution {
        public ListNode mergeKLists(ListNode[] lists) {
            ListNode ans = null;
            for (int i = 0; i < lists.length ;i ++) {
                ans = mergeList(ans, lists[i]);
            }
            return ans;
        }

        public ListNode mergeList(ListNode l1, ListNode l2) {
            ListNode dummy = new ListNode(0);

            ListNode cur = dummy;

            while (l1 != null && l2 != null) {
                int val1 = l1.val == 0 ? 0 : l1.val;
                int val2 = l2.val == 0 ? 0 : l2.val;
                if (val1 < val2) {
                    cur.next = l1;
                    cur = cur.next;
                    l1 = l1.next;
                } else {
                    cur.next = l2;
                    cur = cur.next;
                    l2 = l2.next;
                }
            }

            // 极端情况考虑
            if (l1 == null) {
                cur.next = l2;
            } else {
                cur.next = l1;
            }

            return dummy.next;
        }
    }

    public static class Solution2 {
        /**
         * 数组 分为两个数组
         * @param lists
         * @return
         */
        public ListNode mergeKLists(ListNode[] lists) {

            return merge(lists, 0, lists.length - 1);
        }

        public ListNode merge(ListNode [] lists, int l , int r) {
            if (l == r) {
                return lists[l];
            }
            if (l > r) {
             return null;
            }

            int mid = (l + r) / 2 ;
            return mergeTwoLists(merge(lists, 0, mid), merge(lists, mid+1, r));

        }

        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            // 创建一个空的头结点用来连接第一个节点。
            ListNode dummy = new ListNode(0);

            ListNode cur = dummy;

            /**
             * 遍历排序，l1 与 l2比较。
             */
            while (l1 != null && l2 != null) {
                int val1 = l1.val != 0 ? l1.val : 0;
                int val2 = l2.val != 0 ? l2.val : 0;

                if (val1 < val2) {
                    // l1 < l2
                    // l1后移再次重新比较
                    cur.next = l1;

                    l1 = l1.next;
                } else {
                    // l1 >= l2
                    // l2后移再次重新比较
                    cur.next = l2;
                    l2 = l2.next;
                }
                cur = cur.next;
            }

            // 极端情况考虑
            if (l1 == null) {
                cur.next = l2;
            } else {
                cur.next = l1;
            }

            return dummy.next;
        }

    }
}
